package com.serenegiant.utils;

import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;

public class HandlerThreadHandler extends Handler {
    private static final String TAG = "HandlerThreadHandler";

    private final boolean mAsynchronous;
    private final long mId;

    public static HandlerThreadHandler createHandler() {
        return createHandler("HandlerThreadHandler", false);
    }

    public static HandlerThreadHandler createHandler(boolean mAsynchronous) {
        return createHandler("HandlerThreadHandler", mAsynchronous);
    }

    public static HandlerThreadHandler createHandler(String name) {
        return createHandler(name, false);
    }

    public static HandlerThreadHandler createHandler(String name, boolean mAsynchronous) {
        HandlerThread handlerThread = new HandlerThread(name);
        handlerThread.start();
        return new HandlerThreadHandler(handlerThread.getLooper(), mAsynchronous);
    }

    public static HandlerThreadHandler createHandler(Callback callback) {
        return createHandler("HandlerThreadHandler", callback);
    }

    public static HandlerThreadHandler createHandler(Callback callback, boolean mAsynchronous) {
        return createHandler("HandlerThreadHandler", callback, mAsynchronous);
    }

    public static HandlerThreadHandler createHandler(String name, Callback callback) {
        HandlerThread handlerThread = new HandlerThread(name);
        handlerThread.start();
        return new HandlerThreadHandler(handlerThread.getLooper(), callback, false);
    }

    public static HandlerThreadHandler createHandler(String name, Callback callback, boolean mAsynchronous) {
        HandlerThread handlerThread = new HandlerThread(name);
        handlerThread.start();
        return new HandlerThreadHandler(handlerThread.getLooper(), callback, mAsynchronous);
    }

    private HandlerThreadHandler(Looper looper, boolean mAsynchronous) {
        super(looper);
        this.mId = looper.getThread().getId();
        this.mAsynchronous = mAsynchronous;
    }

    private HandlerThreadHandler(Looper looper, Callback callback, boolean mAsynchronous) {
        super(looper, callback);
        this.mId = looper.getThread().getId();
        this.mAsynchronous = mAsynchronous;
    }

    @Override
    public boolean sendMessageAtTime(Message message, long uptimeMillis) {
        if (this.mAsynchronous && Build.VERSION.SDK_INT >= 22) {
            message.setAsynchronous(true);
        }
        return super.sendMessageAtTime(message, uptimeMillis);
    }

    public long getId() {
        return this.mId;
    }

    public void quitSafely() throws IllegalStateException {
        getLooper().quitSafely();
    }

    public void quit() throws IllegalStateException {
        getLooper().quit();
    }

    public boolean isCurrentThread() throws IllegalStateException {
        return this.mId == Thread.currentThread().getId();
    }
}
